#==========================================================================
# Koala PIM
#--------------------------------------------------------------------------
# (c) 2011  Kumaran Santhanam
#
# This project is released under the GNU General Public License.
# Please see the file COPYING for more details.
#--------------------------------------------------------------------------
# Project makefile
#==========================================================================

VERSION = 0.6.0

#==========================================================================
# DEFINITIONS
#==========================================================================
PACKAGES := kdepimlibs kdepim kcaldav kcarddav
INSTALL  ?= install/strip

BUILD          := _build
ABSBUILD       := $(shell echo `mkdir -p $(BUILD); cd $(BUILD); pwd`)
KOALA_BUILD    := $(ABSBUILD)/install/usr/koala
CMAKE_MODULES  := $(KOALA_BUILD)/lib/cmake
CMAKE_INCLUDES := $(KOALA_BUILD)/include:$(KOALA_BUILD)/include/KDE
CMAKE_CXXFLAGS := -I$(KOALA_BUILD)/include -I$(KOALA_BUILD)/include/KDE

CMAKE := cmake \
           -DCMAKE_MODULE_PATH:PATH=$(CMAKE_MODULES) \
           -DCMAKE_INCLUDE_PATH:PATH=$(CMAKE_INCLUDES) \
           -DSITE:STRING=koala \
	   -DCMAKE_INSTALL_PREFIX:PATH=/usr/koala \
           -DCMAKE_CXX_FLAGS:STRING="$(CMAKE_CXXFLAGS)" \
           -DCMAKE_SKIP_RPATH:BOOL=YES

PACKAGE = koala-$(VERSION)


#==========================================================================
# BUILD RULES
#==========================================================================
build: prep cmake build-bin
	@for pkg in $(PACKAGES); do \
	    echo BUILD: $$pkg; \
	    cd $(BUILD)/$$pkg; \
	    make -k -j8; \
	    make DESTDIR=$(ABSBUILD)/install $(INSTALL); \
	    cd ../..; \
	done
	@cp -af $(BUILD)/install/usr/koala/share/apps/kontact \
                $(BUILD)/install/usr/koala/share/apps/koala

build-bin:
	@mkdir -p $(BUILD)/install/usr/bin
	@mkdir -p $(BUILD)/install/usr/koala/bin
	@mkdir -p $(BUILD)/install/usr/koala/share/icons
	@mkdir -p $(BUILD)/install/usr/share/applications
	@cp -f bin/koala $(BUILD)/install/usr/bin
	@cp -f bin/koala.sh $(BUILD)/install/usr/koala/bin
	@cp -f bin/koala.svg $(BUILD)/install/usr/koala/share/icons
	@cp -f bin/koala.desktop $(BUILD)/install/usr/share/applications

build-%: prep
	@echo BUILD: $(@:build-%=%)
	@make -C $(@:build-%=$(BUILD)/%) \
            DESTDIR=$(ABSBUILD)/install $(INSTALL)

prep:
	@make -C kcaldav OUTDIR=../$(BUILD)/kcaldav \
            ../$(BUILD)/kcaldav/libcaldav.a

cmake:
	@for pkg in $(PACKAGES); do \
	    mkdir -p $(BUILD)/$$pkg; \
	    cd $(BUILD)/$$pkg; \
	    if [ ! -r CMakeCache.txt ]; then \
	        echo CMAKE: $$pkg; \
                $(CMAKE) ../../$$pkg; \
                if [ $$? != 0 ]; then \
                    rm -f CMakeCache.txt; \
                    exit 1; \
                fi; \
            fi; \
            cd ../..; \
	done

cmake-force:
	@for pkg in $(PACKAGES); do \
	    mkdir -p $(BUILD)/$$pkg; \
	    cd $(BUILD)/$$pkg; \
	    echo CMAKE: $$pkg; \
            $(CMAKE) ../../$$pkg; \
            cd ../..; \
	done

debug:
	make INSTALL=install


#==========================================================================
# INSTALL RULES
#==========================================================================
install:
	@echo INSTALL: $(DESTDIR)/usr/koala
	@mkdir -p $(DESTDIR)/usr/koala
	@cp -vr $(BUILD)/install/usr/koala/* $(DESTDIR)/usr/koala
	@chmod -R go-rwx $(DESTDIR)/usr/koala
	@chmod -R go+rX $(DESTDIR)/usr/koala
	@if [ "$$USER" = "root" ]; then chown -R root.root $(DESTDIR)/usr/koala; fi
	@mkdir -p $(DESTDIR)/usr/bin
	@install -m 755 $(BUILD)/install/usr/bin/koala $(DESTDIR)/usr/bin
	@mkdir -p $(DESTDIR)/usr/share/applications
	@install -m 644 $(BUILD)/install/usr/share/applications/koala.desktop \
            $(DESTDIR)/usr/share/applications


#==========================================================================
# PACKAGE RULES
#==========================================================================
tar:
	@echo TAR: $(BUILD)/$(PACKAGE).tar.gz
	@mkdir -p $(BUILD)
	@rm -f $(BUILD)/$(PACKAGE); ln -s .. $(BUILD)/$(PACKAGE)
	@cd $(BUILD); tar --exclude='*/_*' --owner=root --group=root --mtime="`date '+%F %T'`" -czf $(PACKAGE).tar.gz $(PACKAGE)/*

deb: tar
	@echo DEB: $(PACKAGE)
	@rm -fr $(BUILD)/dpkg
	@mkdir -p $(BUILD)/dpkg
	@tar -C $(BUILD)/dpkg -xzf $(BUILD)/$(PACKAGE).tar.gz
	@mkdir -p $(BUILD)/dpkg/$(PACKAGE)/debian
	@for i in pkg/*.dpkg; do \
            cp $$i $(BUILD)/dpkg/$(PACKAGE)/debian/$$(basename $$i .dpkg); \
        done
	@cp COPYING $(BUILD)/dpkg/$(PACKAGE)/debian/copyright
	@cd $(BUILD)/dpkg/$(PACKAGE); dpkg-buildpackage -b -uc
	@cp $(BUILD)/dpkg/*.deb $(BUILD)
